package com.tenement.service.impl;

import cn.hutool.core.util.StrUtil;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryWrapper;
import com.mybatisflex.core.update.UpdateChain;
import com.mybatisflex.spring.service.impl.ServiceImpl;
import com.tenement.entity.Repair;
import com.tenement.mapper.RepairMapper;
import com.tenement.service.RepairService;
import com.tenement.vo.QueryVo;
import com.tenement.vo.RepairVo;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import static com.tenement.entity.table.HouseTableDef.HOUSE;
import static com.tenement.entity.table.RepairTableDef.REPAIR;
import static com.tenement.entity.table.UserTableDef.USER;

/**
 *  服务层实现。
 *
 * @author BertXie
 * @since 1.0.1
 */
@Service
@RequiredArgsConstructor
public class RepairServiceImpl extends ServiceImpl<RepairMapper, Repair> implements RepairService {

    private final RepairMapper repairMapper;


    @Override
    public Page<Repair> list(QueryVo queryVo) {
        QueryWrapper queryWrapper = QueryWrapper.create().from(REPAIR)
                .select(
                        REPAIR.ID, REPAIR.HOUSE_ID ,REPAIR.PROBLEM_DESCRIPTION,
                        REPAIR.PROBLEM_TYPE, REPAIR.ATTACHMENT_URL,REPAIR.REMARK,
                        REPAIR.REPORT_TIME, REPAIR.MAINTENANCE_STAFF_ID, REPAIR.STATUS,
                        REPAIR.COMPLETION_TIME, REPAIR.SCORE , REPAIR.USER_ID,
                        REPAIR.APPRAISE, USER.REAL_NAME, USER.PHONE_NUMBER, USER.ID ,
                        HOUSE.FLOOR_NAME , HOUSE.FLOOR , HOUSE.UNIT_NUMBER ,
                        HOUSE.BUILDING, HOUSE.ID
                )
                .where(
                        REPAIR.PROBLEM_DESCRIPTION.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord()))
                                .or(REPAIR.PROBLEM_DESCRIPTION.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(REPAIR.REMARK.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(USER.PHONE_NUMBER.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(USER.REAL_NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(HOUSE.BUILDING.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(HOUSE.FLOOR_NAME.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(HOUSE.UNIT_NUMBER.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                                .or(HOUSE.FLOOR.like(queryVo.getKeyWord(), StrUtil.isNotEmpty(queryVo.getKeyWord())))
                )
                .leftJoin(HOUSE).on(HOUSE.ID.eq(REPAIR.HOUSE_ID))
                .leftJoin(USER.as("repairUser")).on(USER.ID.eq(REPAIR.MAINTENANCE_STAFF_ID).or(USER.ID.eq(REPAIR.USER_ID)))
                .and(REPAIR.STATUS.eq(queryVo.getStatus(), queryVo.getStatus() != -1));
        return repairMapper.paginate(queryVo.getPage(), queryVo.getSize(), queryWrapper);
    }

    @Override
    public boolean assign(RepairVo repairVo) {
        return UpdateChain.of(Repair.class)
                .set(REPAIR.MAINTENANCE_STAFF_ID, repairVo.getUserId())
                .set(REPAIR.STATUS, 1)
                .where(REPAIR.ID.eq(repairVo.getRepairId())).update();
    }

    @Override
    public boolean changeStatus(Repair repair) {
        return UpdateChain.of(Repair.class)
                .set(REPAIR.STATUS, repair.getStatus())
                .set(REPAIR.REMARK, repair.getRemark())
                .set(REPAIR.MAINTENANCE_STAFF_ID, repair.getMaintenanceStaffId(), repair.getMaintenanceStaffId() != null)
                .where(REPAIR.ID.eq(repair.getId()))
                .update();
    }

    @Override
    public long waitAssign() {
        QueryWrapper queryWrapper = QueryWrapper.create().from(REPAIR).where(REPAIR.STATUS.eq(0));
        return repairMapper.selectCountByQuery(queryWrapper);
    }
}
